package net.mcreator.playerhuds.procedures;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.event.TickEvent;

import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;

import javax.annotation.Nullable;

@Mod.EventBusSubscriber
public class MainHudUpdateProcedure {
	@SubscribeEvent
	public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
		if (event.phase == TickEvent.Phase.END) {
			execute(event, event.player);
		}
	}

	public static void execute(Entity entity) {
		execute(null, entity);
	}

	private static void execute(@Nullable Event event, Entity entity) {
		if (entity == null)
			return;
		entity.getPersistentData().putString("HealthNumShow",
				((new java.text.DecimalFormat("0.#").format(entity instanceof LivingEntity _livEnt ? _livEnt.getHealth() : -1)) + "/"
						+ (new java.text.DecimalFormat("0").format(entity instanceof LivingEntity _livEnt ? _livEnt.getMaxHealth() : -1))));
		entity.getPersistentData().putDouble("HealthStripUpdate", (((entity instanceof LivingEntity _livEnt ? _livEnt.getHealth() : -1)
				/ (entity instanceof LivingEntity _livEnt ? _livEnt.getMaxHealth() : -1)) * 79));
		entity.getPersistentData().putDouble("ArmorNumSaveTemp", (entity instanceof LivingEntity _livEnt ? _livEnt.getArmorValue() : 0));
		entity.getPersistentData().putString("ArmorNumShow",
				(new java.text.DecimalFormat("0.#").format(entity.getPersistentData().getDouble("ArmorNumSaveTemp")) + "/" + "20"));
		entity.getPersistentData().putDouble("ArmorStripUpdate", ((entity.getPersistentData().getDouble("ArmorNumSaveTemp") / 20) * 79));
		entity.getPersistentData().putDouble("HungerNumSaveTemp", (entity instanceof Player _plr ? _plr.getFoodData().getFoodLevel() : 0));
		if (entity.getPersistentData().getDouble("HungerNumSaveTemp") == 20) {
			entity.getPersistentData().putDouble("HungerStripMoveUpdate", 0);
		} else {
			entity.getPersistentData().putDouble("HungerStripMoveUpdate", (80 - entity.getPersistentData().getDouble("HungerNumSaveTemp") * 3.95));
		}
		entity.getPersistentData().putString("HungerNumShow",
				(new java.text.DecimalFormat("0.#").format(entity.getPersistentData().getDouble("HungerNumSaveTemp")) + "/" + "20"));
		entity.getPersistentData().putDouble("HungerStripUpdate", ((entity.getPersistentData().getDouble("HungerNumSaveTemp") / 20) * 79));
		entity.getPersistentData().putDouble("AirNumSaveTemp", (entity.getAirSupply()));
		if (entity.getPersistentData().getDouble("AirNumSaveTemp") == 300) {
			entity.getPersistentData().putDouble("AirStripMoveUpdate", 0);
		} else {
			entity.getPersistentData().putDouble("AirStripMoveUpdate", (80 - (entity.getPersistentData().getDouble("AirNumSaveTemp") / 15) * 3.95));
		}
		entity.getPersistentData().putString("AirNumShow",
				(new java.text.DecimalFormat("0.#").format(entity.getPersistentData().getDouble("AirNumSaveTemp") / 15) + "/" + "20"));
		entity.getPersistentData().putDouble("AirStripUpdate", ((entity.getPersistentData().getDouble("AirNumSaveTemp") / 300) * 79));
		entity.getPersistentData().putString("GoldHeartNumShow",
				(new java.text.DecimalFormat("0").format(entity instanceof Player _plr ? _plr.getAbsorptionAmount() : 0)));
		if ((entity instanceof Player _plr ? _plr.getAbsorptionAmount() : 0) > 10) {
			entity.getPersistentData().putDouble("GoldHeartMoveUpdate", 6);
		} else {
			entity.getPersistentData().putDouble("GoldHeartMoveUpdate", 0);
		}
	}
}
